const path = require('path');
const fs = require('fs');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const {
    CleanWebpackPlugin
} = require('clean-webpack-plugin');
const laya = require('./webpack.laya')
const CopyWebpackPlugin = require('copy-webpack-plugin');
// App directory
const appDirectory = fs.realpathSync(process.cwd());
const resolve = function (dir) {
    return path.join(__dirname, '..', dir)
}

module.exports = {
    entry: {
        laya: laya.entry,
        entry: path.resolve(appDirectory, "src/index.ts"),
    },
    output: {
        filename: 'js/[name].js',
    },
    resolve: {
        extensions: ['.ts', '.js']
    },
    module: {
        rules: [
            {
                test: /\.js$/,
                loader: 'babel-loader',
                include: [resolve('src'), resolve('libs')]
            },
            {
                test: /\.(js|mjs|jsx|ts|tsx)$/,
                loader: 'source-map-loader',
                enforce: 'pre',
            },
            {
                test: /\.tsx?$/,
                loader: 'ts-loader'
            },
            {
                test: /\.(png|jpg|gif|env|glb|stl)$/i,
                use: [{
                    loader: 'url-loader',
                    options: {
                        limit: 8192,
                    },
                },],
            }
        ]
    },
    plugins: [
        new CleanWebpackPlugin(),
        new HtmlWebpackPlugin({
            inject: true,
            template: path.resolve(appDirectory, "public/index.html"),
        }),
        new CopyWebpackPlugin({ patterns: [{ from: 'bin/res', to: 'res' }] })
    ],
    // Just for ammo
    node: {
        fs: 'empty'
    }
}